package com.hunting.agl.login;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;


import com.hunting.agl.authority.Authority;
import com.hunting.agl.authority.AuthorityDao;
import com.hunting.agl.register.Register;
import com.hunting.agl.register.RegisterDao;
import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private List<Authority> auths;
	private AuthorityDao authorityDao;
	private Register register;
	private LoginDao loginDao;
	private RegisterDao registerDao;
	private Login login;
	private List<Register> list;
	
	
	public List<Register> getList() {
		return list;
	}

	public void setList(List<Register> list) {
		this.list = list;
	}

	public Login getLogin() {
		return login;
	}

	public void setLogin(Login login) {
		this.login = login;
	}

	public RegisterDao getRegisterDao() {
		return registerDao;
	}

	public void setRegisterDao(RegisterDao registerDao) {
		this.registerDao = registerDao;
	}

	public LoginDao getLoginDao() {
		return loginDao;
	}

	public void setLoginDao(LoginDao loginDao) {
		this.loginDao = loginDao;
	}

	public Register getRegister() {
		return register;
	}

	public void setRegister(Register register) {
		this.register = register;
	}

	public AuthorityDao getAuthorityDao() {
		return authorityDao;
	}

	public void setAuthorityDao(AuthorityDao authorityDao) {
		this.authorityDao = authorityDao;
	}

	public List<Authority> getAuths() {
		return auths;
	}

	public void setAuths(List<Authority> auths) {
		this.auths = auths;
	}
	public String execute(){
		auths=authorityDao.findAll();
		return SUCCESS;
	}
	
	public String saveRegister(){
		registerDao.saveOrUpdate(this.register);
		return SUCCESS;
	}
	
	HttpSession session;
	public String saveLogin(){
		
		if(this.list.size()!=0){	//若注册者（用户）信息不为空，将注册者信息存入session
			session = ServletActionContext.getRequest().getSession();
			session.setAttribute("user",login);
			session.setAttribute("id", list.get(0).getUser_id());
			//System.out.println("3333333333"+((Login)session.getAttribute("user")));
			Login l = (Login) session.getAttribute("user");
			//System.out.println("=======================================");
			
			//若注册的类型为1，返回SUCCESS
			if(((Login)session.getAttribute("user")).getAuthority().getId()==1L){
				return SUCCESS;
			}else{
				//若注册的类型2，返回company
				if(((Login)session.getAttribute("user")).getAuthority().getId()==2L){
					return "company";
				}else{
						return "person";
				}
			}
		}else{	//若注册信息为空，返回input
			return "input";
		}
	}
	
		
	
	public void validateSaveLogin(){
		list=registerDao.findByUPA(this.login.getUserName(),this.login.getPassWord(),
				this.login.getAuthority().getId());
		if(list.size()==0){
			//this.addFieldError(login.getUserName(),"用户名错误");
			this.addFieldError("login.userName", "errorMessage");
			System.err.println("??????????????????");
		}
		System.out.println("<<<<<<<<<"+list.size());
		System.err.println("dkkkkkkkk");
	}
	
//	public String login(){
//		return SUCCESS;
//	}
//	public void validateLogin(){
//		if(loginDao.findByUPA(this.register.getUserName(),
//				this.register.getPassWord(),this.register.getAuthority().getId())==null){
//			
//			
//		}
//	}

}
